package com.jcp.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alipay.api.domain.MMemberLevel;
import com.jcp.entity.MonthMoney;
import com.jcp.entity.Park;
import com.jcp.service.MoneyService;
import com.jcp.service.ParkService;
import com.jcp.util.LayuiData;


/**
 * 车位管理的控制器
 * @author 朱耿封
 *
 */
@Controller
public class ParkController {
	//车位管理的service层的依赖注入
	@Autowired
	private ParkService park1;
	@Autowired
	private  MoneyService moneyService;
	
	/**
	 * 后台首页显示的页面
	 * @param model：视图数据储存器
	 * @return：返回一个页面
	 */
	@GetMapping(value="CarTotel")
	public String CarTotel(Model model) {
		//调用selectPark方法获取Park的对象集合
		List<Park> list = park1.selectPark();
		//存储数据
		int ck=0;
		if(moneyService.selectDeposit()!=null) ck=moneyService.selectDeposit();
		int rent=0;
		if(moneyService.selectRent()!=null) rent=moneyService.selectRent();
		
		int temp=0;
		
		if(moneyService.selectTemp()!=null) temp=moneyService.selectTemp();
		int allck=moneyService.getPaymentT();
		int allrecord=moneyService.getRecordT();
		int all=allck+allrecord;
		
		List<MonthMoney> listR= moneyService.selectMR();
		List<MonthMoney> listP= moneyService.selectMP();
		List<Integer> listt=new ArrayList<Integer>();
		
		for(int i=0;i<=11;i++) {
	    System.out.println("listR.get(i).getXtotal():"+listR.get(i).getXtotal());
		listt.add(i, listR.get(i).getXtotal()+listP.get(i).getXtotal());}
		System.out.println("listt:"+listt.get(4));
		
		model.addAttribute("park", list.get(0));
		model.addAttribute("vip", list.get(0).getVip_park_space());
		model.addAttribute("putong",list.get(0).getOrdinary_park_space());
		model.addAttribute("ck", ck);
		model.addAttribute("rent", rent);
		model.addAttribute("temp", temp);
		model.addAttribute("all", all);
		model.addAttribute("listt", listt);
		
		/*listR.get(0).getXtotal();
		listP.get(0).getXtotal();
		listt.get(0);*/
		
		
		
		//转发页面到welcome.jsp
		return "forward:welcome.jsp";
	}
	
	/**
	 * 车位管理
	 * @param model：视图数据储存器
	 * @return：返回一个页面
	 */
	@GetMapping(value="CheWei")
	@ResponseBody
	public LayuiData<Park> CarGuanLi() {
		
		//调用selectPark方法获取Park的对象集合
		List<Park> list = park1.selectPark();
		LayuiData<Park> ld = new LayuiData<>(0, "", 1, list);
		//存储数据
		//转发页面到welcome.jsp
		return ld;
	}
	
	/**
	 * 车位管理的修改
	 * @return：返回一个页面
	 */
	@PostMapping(value="Update")
	@ResponseBody
	public String UpdateCarGuanLi(@RequestParam(value="ordinary_park_space")int ordinary_park_space,@RequestParam(value="vip_park_space")int vip_park_space,@RequestParam(value="remaining_park_space")int remaining_park_space) {
		//调用方法updatePark进行车位的修改
		int num = park1.updatePark(ordinary_park_space, vip_park_space, remaining_park_space);
		//用Ajax返回true或false
		return num>0?"true":"false";
	}
	/**
	 * 退出
	 * @return
	 */
	@GetMapping(value="Exit")
	private String Exit(HttpServletRequest request) {
		// TODO Auto-generated method stub
		//session.remove();
		request.getSession().removeAttribute("user");
		
		return "forward:Login.jsp";
	}
	
}
